<template>
	<view class="address-manage rf-row-wrapper">
		<view class="row b-b">
			<text class="tit">姓名</text>
			<input
				class="input"
				type="text"
				v-model="cstmData.customName"
				@tap="handleCstm"
				placeholder="姓名"
				placeholder-class="placeholder"
				disabled="true"
			/>
		</view>
		
		<view  class="row b-b" >
			<text class="tit">出生日期</text>
			<picker mode="date"  v-model="drugData.birthday"  @change="bindBeginDateChange">
				<view>{{drugData.birthday}}</view>
			</picker>
		</view>	
			
			
		<view  class="row b-b"> 
			<text class="tit">提醒方式</text>
			<radio-group @change="radioChange">
				<label class="gender-item">
					<radio value="1"  :checked="drugData.remindType === 1"  style="transform:scale(0.7)" :color="themeColor.color" /><text class="gender-item-text">生日前天</text>
				</label>
				<label class="gender-item">
					<radio value="2"  :checked="drugData.remindType === 2"  style="transform:scale(0.7)" :color="themeColor.color" /><text class="gender-item-text">生日当天</text>
				</label>				
			</radio-group>
		</view>	
					
		<view class="row b-b">
			<text class="tit">短信内容</text>
			<input
				class="input"
				type="text"
				v-model="drugData.sendContext"
				placeholder="短信内容"
				placeholder-class="placeholder"				
			/>
			
		</view>		
					
		<view class="row b-b">
			<text class="tit">提醒时间</text>
			<picker mode="time" id="morning"  v-model="drugData.remindTime" start="07:30" end="11:30" @change="bindTimeChange">
				<view>{{drugData.remindTime}}</view>
			</picker>
	
		</view>					
		
		<view v-if="false" class="row b-b" >
			<text class="tit">手机号</text>
			<input
				class="input"
				type="number"
				v-model="cstmData.homeTel"
				placeholder="接受短信手机号码"
				placeholder-class="placeholder"
				disabled="true"
			/>
		</view>
		<view v-if="false" class="row b-b">
			<text class="tit">药品</text>
			<input
				class="input"
				type="text"
				v-model="drugData.drugName"
				placeholder="药品"
				placeholder-class="placeholder"
				
			/>
			
		</view>		
		<view v-if="false" class="row b-b">
			<text class="tit">短信内容</text>
			<input
				class="input"
				type="text"
				v-model="drugData.sendContext"
				placeholder="短信内容"
				placeholder-class="placeholder"				
			/>
			
		</view>
				
		<view v-if="false" class="row b-b" >
			<text class="tit">开始日期</text>
			<picker mode="date"  v-model="drugData.startTime"  @change="bindBeginDateChange">
				<view>{{drugData.startTime}}</view>
			</picker>
		</view>
		
		
		<view v-if="false" class="row b-b">
			<text class="tit">结束日期</text>
			<picker mode="date"  v-model="drugData.endTime"  @change="bindStopDateChange">
				<view>{{drugData.endTime}}</view>
			</picker>	
		</view>
			
		
		<view v-if="false" class="row b-b">
			<text class="tit">提醒时间（早）</text>
			<picker mode="time" id="morning"  v-model="drugData.morning" start="07:30" end="11:30" @change="bindTimeChange">
				<view>{{drugData.morning}}</view>
			</picker>
	
		</view>
		
		<view v-if="false" class="row b-b">
			<text class="tit">提醒时间（中）</text>
			<picker mode="time" id="noon"  v-model="drugData.noon" start="11:30" end="17:30" @change="bindTimeChange">
				<view>{{drugData.noon}}</view>
			</picker>			
		</view>
		
		<view v-if="false" class="row b-b">
			<text class="tit">提醒时间（晚）</text>
			<picker mode="time" id="evening" v-model="drugData.evening" start="17:30" end="23:29" @change="bindTimeChange">
				<view>{{drugData.evening}}</view>
			</picker>				
		</view>		
		<view v-if="false" class="row b-b"> 
			<text class="tit">用药时间</text>
			<radio-group @change="radioChange">
				<label>
					<radio value="1"  :checked="drugData.medicationType === 1" /><text>饭钱</text>
				</label>
				<label>
					<radio value="2"  :checked="drugData.medicationType === 2"  /><text>饭后</text>
				</label>				
			</radio-group>
		</view>		
		
		<view class="row default-row">
			<text class="tit">立即启用</text>
			<switch
				:checked="drugData.state === 1 ? true : false"
				:color="themeColor.color"
				@change="switchChange"
			/>
		</view>
		<button
			class="add-btn"
			:class="'bg-' + themeColor.name"
			:disabled="btnLoading"
			:loading="btnLoading"
			@tap="confirm"
		>
			提交
		</button>

		<!--加载动画-->
		<rfLoading isFullScreen :active="loading"></rfLoading>
	</view>
</template>

<script>
/**
 * @des 用药提醒列表
 *
 * @author stav stavyan@qq.com
 * @date 2020-03-10 18:00
 * @copyright 2019
 */
import { saveBirthdayRemind, getCstmBirthdaySet } from '@/api/userInfo';
import rfPickRegions from '@/components/rf-pick-regions';
	function getDate(type) {
		const date = new Date();
	
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
	
		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
	
		return `${year}-${month}-${day}`;
	}
export default {
	components: {
		rfPickRegions
	},
	data() {
		return {
			drugData: {},
			cstmData : {
			},
			btnLoading: false,
			multiArray: [[], [], []],
			multiIndex: [0, 0, 0],
			loading: true,
			userInfo : {}
		};
	},
	onLoad(options) {
		this.initData(options);
	},
	methods: {
		radioChange: function(e) {
			this.drugData.remindType = e.detail.value;
		},
		bindTimeChange: function(e) {
			this.time = e.detail.value
		},
		//跳转
		handleCstm(e){
			this.$mRouter.push({
				route: `/pages/user/family/family?source=1`
			});			
		},
		// 获取选择的地区
		handleGetRegions(e) {
			this.drugData.provinceIds = e.provinceIds;
			this.drugData.cityIds = e.cityIds;
			this.drugData.countyIds = e.countyIds;
		},
		// 数据初始化
		async initData(options) {
			this.userInfo = uni.getStorageSync('userInfo');			
			let title = '新增生日提醒';
			if (options.type === 'edit') {
				title = '编辑生日提醒';
				await this.getAddressDetail(options.id);
			} else {
				setTimeout(() => {
					this.loading = false;
				}, 1 * 1000);
			}
			this.manageType = options.type;
			uni.setNavigationBarTitle({
				title
			});
		},
		// 获取用药提醒
		async getAddressDetail(id) {
			var params = {
			   corpId: this.userInfo && this.userInfo.corpId,				
               userId : this.userInfo && this.userInfo.userId,
               cstmId: id			   
          }
			await this.$http
				.get(`${getCstmBirthdaySet}`, params)
				.then(async r => {
					console.log("生日详情返回: " + JSON.stringify(r.data));
					this.drugData = await r.data;
					this.cstmData.id = r.data.cstmId;
					this.cstmData.customName = r.data.cstmName;
					this.loading = false;
				})
				.catch(() => {
					this.loading = false;
				});
		},
		handleRealNameChange(e) {
			this.drugData.cstmName = e.detail.value;
		},
		bindAddressDetailsChange(e) {
			this.drugData.homeAddress = e.detail.value;
		},
		handleMobileChange(e) {
			this.drugData.telNums = e.detail.value;
		},
		switchChange(e) {
			this.drugData.state = e.detail.value ? 1 : 2;
		},
		bindBeginDateChange: function(e) {
			this.drugData.birthdayGl = e.detail.value
		},
		bindStopDateChange:function(e){
			this.drugData.endTime = e.detail.value
				
		},
		bindTimeChange: function(e) {
			this.drugData.remindTime = e.detail.value			
		},
		// 提交
		confirm() {
			let data = this.drugData;
			if (!this.cstmData.customName) {
				this.$mHelper.toast('请选择家人');
				return;
			}
			/*
			if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(this.cstmData.homeTel)) {
				this.$mHelper.toast('请输入正确的手机号码');
				return;
			}*/			
			if (!data.birthday) {
				this.$mHelper.toast('请选择出生日期');
				return;
			}
			if (!data.sendContext) {
				this.$mHelper.toast('请输入生日祝福短信内容');
				return;
			}
			this.btnLoading = true;
			if (this.manageType === 'edit') {
				this.handleAddressUpdate(data);
			} else {
				this.handleAddressCreate(data);
			}
		},
		async handleAddressUpdate(data) {			
			await this.$http
				.get(`${saveBirthdayRemind}`, {
					userId : this.userInfo && this.userInfo.userId,
					userType:this.userInfo && this.userInfo.userType,
					corpId : this.userInfo && this.userInfo.corpId,
					cstmId:	this.cstmData.id,
					cstmName : this.cstmData.customName,
					sendContext:data.sendContext,
					glDate:data.birthday,
					nlDate:'',
					remindTime : data.remindTime,
					state:data.state,
					remindType:data.remindType,
					
				})
				.then(r => {
					if(r.data.success){
						this.btnLoading = false;
						this.$mHelper.toast('生日提醒设置成功！');
						this.$mRouter.back();						
					}else{
						this.btnLoading = false;
						this.$mHelper.toast(r.data.msg);
					}

				})
				.catch(() => {
					this.btnLoading = false;
				});
		},
		async handleAddressCreate(data) {

			await this.$http
				.get(`${editDrug}`, {
					userId : this.userInfo && this.userInfo.userId,
					userType:this.userInfo && this.userInfo.userType,
					cstmId:	this.cstmData.id,
					cstmName : this.cstmData.customName,
					sendContext:data.sendContext,
					startDate : data.startTime,
					endDate:data.endTime,
					drugName:data.drugName,
					morning : data.morning,
					noon:data.noon,
					evening:data.evening,
					state:data.state,
					drugTime:data.medicationType
				})
				.then(r => {
					if(r.data.success){
						this.btnLoading = false;
						this.$mHelper.toast('用药提醒修改成功！');
						this.$mRouter.back();
					}else{
						this.btnLoading = false;
						this.$mHelper.toast(r.data.msg);
					}

				})
				.catch(() => {
					this.btnLoading = false;
				});
		}
	}
};
</script>

<style lang="scss">
page {
	background: $page-color-base;
}
.address-manage {
	padding-top: 16upx;
}
.gender-item {
	margin-right: 20upx;

	.gender-item-text {
		padding-left: 10upx;
	}

	radio .wx-radio-input.wx-radio-input-checked {
		background: $uni-color-primary;
		border-color: $uni-color-primary;
	}
}
</style>
